package cn.tedu.controller;

import cn.tedu.dao.BannerDao;
import cn.tedu.dao.CategoryDao;
import cn.tedu.dao.ProductDao;
import cn.tedu.entity.Banner;
import cn.tedu.entity.Category;
import cn.tedu.entity.Product;
import cn.tedu.entity.User;
import cn.tedu.utils.ThUtils;
import org.thymeleaf.context.Context;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "HomeServlet",urlPatterns = "/home")
public class HomeServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //创建分类Dao 查询所有分类
        CategoryDao dao = new CategoryDao();
        List<Category> list =dao.findAll();
        System.out.println(list);

        Context context = new Context();
        context.setVariable("list",list);



        //取出保存到Session对象里面的user
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if(user!=null){

            System.out.println("登陆过");
        }else{
            System.out.println("没登录");

        }
        context.setVariable("user",user);

        //把去除的用户对象保存到容器中 如果登陆过user有值 没有登录过 则没有

        //轮播图
        BannerDao bDao = new BannerDao();
        List<Banner> bList = bDao.findAll();
        context.setVariable("bList",bList);
        System.out.println(bList);

        //首页展示作品
        ProductDao pDao = new ProductDao();
        List<Product> pList= pDao.findAll();
        context.setVariable("pList",pList);






        ThUtils.print("home.html",context,response);

    }
}
